import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLDataException;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入学号");
//        int id = scanner.nextInt();
//        scanner.nextLine();
//        System.out.println("请输入姓名");
//        String name = scanner.nextLine();
        //创建DataSouse
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/my_code?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //建立与服务器之间的连接

        Connection connection = dataSource.getConnection();

        //构造sql
        //String sql1 = "insert into student values("+ id +",'"+ name +"')";
        String sql1 = "insert into student values(?,?)";
        //String sql1 = "delete from student where id = 1";
        PreparedStatement statement = connection.prepareStatement(sql1);
        statement.setInt(1,888);
        statement.setString(2,"牛");
        //把sql发送给服务器,返回值是一个整数，表示影响行数
        int n = statement.executeUpdate();
        System.out.println("n = " + n);
        //释放资源
        statement.close();
        connection.close();
        scanner.close();
    }
}